# Social Mobilisation in Partisan Spaces
# #Appendix: Replication Materials
# Florian Foos, Peter John, Christian Mueller, and Kevin Cunningham

# Questions to f.foos@lse.ac.uk

#install.packages("tidyverse", dependencies = TRUE)
#install.packages("interflex", dependencies = TRUE)
#install.packages("car", dependencies = TRUE)
#library("dplyr")
#library("lmtest")
#install.packages("readstata13")
#install.packages("ri")
#install.packages("randomizr")
#install.packages("estimatr")
#install.packages("texreg")
#install.packages("descr")
#install.packages("plyr")


library(tidyverse)
library(interflex)
library(car)
library(dplyr)
library(lmtest)
library(readstata13)
library(ri)
library(randomizr)
library(estimatr)
library(texreg)
library(descr)
library(plyr)


rm(list=ls())

options(scipen=999)

setwd("")

dataset <- as.data.frame(readRDS("data.rds"))

names(dataset)

dataset$include<-NA
dataset$include[dataset$latest_voter_id_observed==1]<-1
dataset$include[dataset$addr_hh_size>10]<-NA
dataset$include[dataset$unable_to_vote==TRUE]<-NA

dataset<-subset(dataset,!is.na(include))

dataset$include1<-NA
dataset$include1[dataset$latest_voter_id_observed==1]<-1

dataset<-subset(dataset,!is.na(include1))

dataset$include2<-NA
dataset$include2[dataset$experiment_sample== "indirect"]<-1

dataset$include3<-NA
dataset$include3[dataset$experiment_sample== "direct"]<-1

#Table A1

tab_pid <- dataset %>%
  filter(!is.na(marked_register_2014)) %>%
  group_by(experiment_sample, latest_voter_id_labour, latest_voter_id_conservative, latest_voter_id_rivalparty, latest_voter_id_nonvoter, latest_voter_id_other) %>%
  tally() %>%
  gather(row, value, -experiment_sample, -n) %>%
  filter(value == 1) %>%
  select(-value) %>%
  spread(experiment_sample, n) %>%
  mutate(
    total = direct + indirect,
    direct_prop = direct / sum(direct),
    indirect_prop = indirect / sum(indirect)
  )

summarise(
  tab_pid,
  direct = sum(direct),
  direct_prop = sum(direct_prop),
  indirect = sum(indirect),
  indirect_prop = sum(indirect_prop),
  total = sum(total)
) %>%
  cbind(tibble(row = "total")) %>%
  rbind(tab_pid, .) %>%
  mutate_at(c("direct", "indirect", "total"), list(~formatC(., big.mark = ",", format = "d"))) %>%
  mutate(
    row = str_replace(row, "latest_voter_id_", ""),
    row = factor(row, levels = c("conservative", "labour",  "nonvoter", "other", "rivalparty",  "total"))
  ) %>%
  arrange(row) %>%
  select(row, direct, direct_prop, indirect, indirect_prop, total) %>%
  xtable::xtable() %>%
  xtable::print.xtable(include.rownames = FALSE)

spillover<-subset(dataset,!is.na(include2))
direct<-subset(dataset,!is.na(include3))

spillover$party<-NA
spillover$party[spillover$latest_voter_id_labour==1]<-1
spillover$party[spillover$latest_voter_id_rivalparty==1]<-1
spillover$party[spillover$latest_voter_id_conservative==1]<-1

direct$party<-NA
direct$party[direct$latest_voter_id_labour==1]<-1
direct$party[direct$latest_voter_id_rivalparty==1]<-1
direct$party[direct$latest_voter_id_conservative==1]<-1

spillover<-subset(spillover,!is.na(party))
direct<-subset(direct,!is.na(party))

nrow(spillover)
nrow(direct)

spillover$male<-NA
spillover$male[spillover$gender=="F"]<-0
spillover$male[spillover$gender=="U"]<-0
spillover$male[spillover$gender=="M"]<-1

spillover$labour<-0
spillover$labour[spillover$latest_voter_id_labour==1]<-1

spillover$rival<-0
spillover$rival[spillover$latest_voter_id_rivalparty==1]<-1

spillover$cons<-0
spillover$cons[spillover$latest_voter_id_conservative==1]<-1

spillover$attrition<-NA
spillover$attrition[is.na(spillover$marked_register_2014)]<-1
spillover$attrition[!is.na(spillover$marked_register_2014)]<-0

spillover$treat<-NA
spillover$treat[spillover$treatment_assign=="treated"]<-1
spillover$treat[spillover$treatment_assign=="control"]<-0

spillover_sub<-subset(spillover, !is.na(marked_register_2014),  select=c(street_rival_share, street, ward, treat, marked_register_2014, marked_register_2013, addr_hh_size, marked_register_2013_nomiss, male, labour, rival, cons))


#Figure A5

pdf(paste("hist.pdf"),w=6,h=5)
hist(spillover_sub$street_rival_share,
     main="Share of rival partisan per street",
     xlab="proportion of rival partisans",
     border="black",
     col="white",
     xlim=c(0,1),
     las=1,
     breaks=100)
dev.off()


#Attrition

Z<-spillover$treat

Y <- spillover$attrition

blockvar<-spillover$ward

cluster<-spillover$street


library(ri)

set.seed(12345678)

probs <- genprobexact(Z, blockvar=blockvar, clustvar=cluster)
probs

w <- Z/probs + (1-Z)/(1-probs)

crosstab(Z, Y, weight=w, prop.r= TRUE)

ate <- estate(Y,Z,prob=probs)
ate

perms <- genperms(Z,maxiter=5000, blockvar=blockvar, clustvar=cluster)

Ys <- genouts(Y,Z,ate=0)

distout <- gendist(Ys,perms,prob=probs)
dispdist(distout,ate) #0.3968


#Figure A5

pdf(paste("attrition_dif.pdf"),w=6,h=5)
distout <- gendist(Ys,perms,prob=probs)
dispdist(distout,ate)
dev.off()


#Balance

set.seed(12345678)

individual_level <- spillover_sub
data_treatment <- spillover_sub[order(spillover_sub$street),]
household_level <- ddply(individual_level, c("ward","street"), summarise, treatment=mean(treat))
with(household_level, table(ward, treatment))  ## This is how many treatments to allocate.

constituency_ra <- function(){
  household_level <- within(household_level,{
    Z_sim <- rep(NA, nrow(household_level))
    Z_sim[ward=="A"] <- complete_ra(N = sum(ward=="A"), m_each=c(27, 70))
    Z_sim[ward=="B"] <- complete_ra(N = sum(ward=="B"), m_each=c(17, 46))
    Z_sim[ward=="C"] <- complete_ra(N = sum(ward=="C"), m_each=c(23, 57))
    Z_sim[ward=="D"] <- complete_ra(N = sum(ward=="D"), m_each=c(17, 45))
    Z_sim[ward=="E"] <- complete_ra(N = sum(ward=="E"), m_each=c(14, 35))
    Z_sim[ward=="F"] <- complete_ra(N = sum(ward=="F"), m_each=c(11, 37))
    Z_sim[ward=="G"] <- complete_ra(N = sum(ward=="G"), m_each=c(24,  61))
    Z_sim[ward=="H"] <- complete_ra(N = sum(ward=="H"), m_each=c(11, 30))
    Z_sim[ward=="I"] <- complete_ra(N = sum(ward=="I"), m_each=c(16,  52))
  })
  individual_level <- join(individual_level, household_level, by = c("ward", "street"))
  return(individual_level$Z_sim)
}


#simulation

Z<-individual_level$treat
blockvar<-individual_level$ward
clustvar<-individual_level$street
X <- as.matrix(individual_level[,7:12])

probs <- genprobexact(Z, blockvar=blockvar, clustvar=clustvar)
probs

w <- Z/probs + (1-Z)/(1-probs)


#Table A2

mean1<-mean(individual_level$addr_hh_size[Z==0], weight=w[Z==0])
mean1
mean2<-mean(individual_level$addr_hh_size[Z==1], weight=w[Z==1])
mean2
tab3<-crosstab(Z, individual_level$male, weight=w, prop.r= TRUE)
tab3
tab4<-crosstab(Z, individual_level$marked_register_2013, weight=w, prop.r= TRUE)
tab4
tab5<-crosstab(Z, individual_level$labour, weight=w, prop.r= TRUE)
tab5
tab6<-crosstab(Z, individual_level$rival, weight=w, prop.r= TRUE)
tab6
tab7<-crosstab(Z, individual_level$cons, weight=w, prop.r= TRUE)
tab7


nsims <- 5000
set.seed(12345678)


fstatstore <- rep(NA,nsims)

for (i in 1:nsims) {
  fstatstore[i] <- summary(lm(constituency_ra()~X,weight=w))$fstatistic[1]
}

fstat <- summary(lm(Z~X, weight=w))$fstatistic[1]

mean(fstat<= fstatstore) #0.175


#Figure A6

pdf(paste("balance.pdf"),w=6,h=5)
hist(fstatstore, breaks = 100, main = paste("Sampling distribution of the estimated f-statistic"), xlab=("Estimated f-statistic"))
abline(v=fstat, col="red", lwd=3)
dev.off()


#Recode

dataset$indirect<-NA
dataset$indirect[dataset$experiment_sample== "indirect"]<-1

dataset$direct<-NA
dataset$direct[dataset$experiment_sample== "direct"]<-1

dataset$party<-NA
dataset$party[dataset$latest_voter_id_conservative==1]<-1
dataset$party[dataset$latest_voter_id_labour==1]<-1
dataset$party[dataset$latest_voter_id_rivalparty==1]<-1

dataset$labour<-NA
dataset$labour[dataset$latest_voter_id_labour==1]<-1

dataset$rival<-NA
dataset$rival[dataset$latest_voter_id_conservative==1]<-1
dataset$rival[dataset$latest_voter_id_rivalparty==1]<-1

dataset$treat<-NA
dataset$treat[dataset$treatment_assign=="treated"]<-1
dataset$treat[dataset$treatment_assign=="control"]<-0

dataset$male<-NA
dataset$male[dataset$gender=="M"]<-1
dataset$male[dataset$gender=="F"]<-0
dataset$male[dataset$gender=="U"]<-0

#subset sample

spillover<-subset(dataset,!is.na(indirect))

direct<-subset(dataset,!is.na(direct))


direct_pid<-subset(direct,!is.na(include))

spillover_pid<-subset(spillover,!is.na(include))

direct_pid<-subset(direct_pid,!is.na(marked_register_2014))

direct_pid2<-subset(direct_pid,!is.na(party))

direct_labour<-subset(direct_pid,!is.na(labour))

spillover_pid<-subset(spillover_pid,!is.na(marked_register_2014))

spillover_pid<-subset(spillover_pid,!is.na(street_rival_share))

spillover_pid2<-subset(spillover_pid,!is.na(party))

spillover_pid3<-subset(spillover_pid2,!is.na(labour))

spillover_pid4<-subset(spillover_pid2,!is.na(rival))



spillover_pid_analysis2<-subset(spillover_pid2,!is.na(match_top1_all_rival))

spillover_pid_analysis3<-subset(spillover_pid3,!is.na(match_top1_all_rival))

spillover_pid_analysis4<-subset(spillover_pid4,!is.na(match_top1_all_rival))


#create IP weights

Z<-spillover_pid$treat

probs <- genprobexact(Z, blockvar=spillover_pid$ward)
probs

spillover_pid$w <- Z/probs + (1-Z)/(1-probs)


Z<-spillover_pid2$treat

probs <- genprobexact(Z, blockvar=spillover_pid2$ward)
probs

spillover_pid2$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid2$treat

probs <- genprobexact(Z, blockvar=spillover_pid2$ward)
probs

spillover_pid2$w <- Z/probs + (1-Z)/(1-probs)


Z<-spillover_pid3$treat

probs <- genprobexact(Z, blockvar=spillover_pid3$ward)
probs

spillover_pid3$w <- Z/probs + (1-Z)/(1-probs)

Z<-spillover_pid4$treat

probs <- genprobexact(Z, blockvar=spillover_pid4$ward)
probs

spillover_pid4$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid_analysis2$treat

probs <- genprobexact(Z, blockvar=spillover_pid_analysis2$ward)
probs

spillover_pid_analysis2$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid_analysis3$treat

probs <- genprobexact(Z, blockvar=spillover_pid_analysis3$ward)
probs

spillover_pid_analysis3$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid_analysis4$treat

probs <- genprobexact(Z, blockvar=spillover_pid_analysis4$ward)
probs

spillover_pid_analysis4$w <- Z/probs + (1-Z)/(1-probs)


Z<-direct_pid$treat

probs <- genprobexact(Z, blockvar=direct_pid$ward)
probs


direct_pid$w <- Z/probs + (1-Z)/(1-probs)


Z<-direct_pid2$treat

probs <- genprobexact(Z, blockvar=direct_pid2$ward)
probs


direct_pid2$w <- Z/probs + (1-Z)/(1-probs)


Z<-direct_labour$treat

probs <- genprobexact(Z, blockvar=direct_labour$ward)
probs


direct_labour$w <- Z/probs + (1-Z)/(1-probs)



#Table A.1 missing

#Figure A.3 missing


#Figure A.4

attach(spillover_pid_analysis2)
pdf(paste("corr.pdf"))

scatter.smooth(street_rival_share, match_top1_all_rival, span = 1, degree = 1,
               family = c("gaussian"), xlab="Share of rival partisans in the same street", ylab="Share of rival partisans in most proximate household")
dev.off()




#Figure A7a

pdf(paste("Figure_all_pre_analysis1.pdf"),w=7,h=5)
inter.binning(Y = "marked_register_2014", D = "treat", X="street_rival_share", FE=("ward"), cl="street", bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", data = spillover_pid, weights = "w")
dev.off()


#Figure A7b

pdf(paste("Figure_all_analysis1.pdf"),w=7,h=5)
inter.binning(Y = "marked_register_2014", D = "treat", X="street_rival_share", FE=("ward"),cl="street", bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", data = spillover_pid2, weights = "w")
dev.off()



#Household level analysis

spillover_pid_analysis2$match_top1_single <- as.numeric(spillover_pid_analysis2$match_top1_all_n_obs == 1)
spillover_pid_analysis2$match_top1_single_interact <- as.numeric(spillover_pid_analysis2$match_top1_single * spillover_pid_analysis2$treat)
spillover_pid_analysis2$match_top1_nobs_interact <- as.numeric(spillover_pid_analysis2$match_top1_all_n_obs * spillover_pid_analysis2$treat)
spillover_pid_analysis2$match_top1_dist_interact <- as.numeric(spillover_pid_analysis2$match_top1_all_dist * spillover_pid_analysis2$treat)
spillover_pid_analysis2$match_top1_dist_interact3 <- as.numeric(spillover_pid_analysis2$match_top1_all_rival*spillover_pid_analysis2$match_top1_all_dist * spillover_pid_analysis2$treat)


all_analysis2<-inter.binning(Y = "marked_register_2014", D = "treat", X="match_top1_all_rival",  Z=c("match_top1_all_dist", "match_top1_single", "match_top1_single_interact", "match_top1_all_n_obs", "match_top1_nobs_interact") , FE=("ward"), cl = "street" , bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel = "share of rival party supporters in closest household", cutoffs = c(0.01,0.99), Xdistr = "density", data = spillover_pid_analysis2, weights = "w")

spillover_pid_analysis3$match_top1_single <- as.numeric(spillover_pid_analysis3$match_top1_all_n_obs == 1)
spillover_pid_analysis3$match_top1_single_interact <- as.numeric(spillover_pid_analysis3$match_top1_single * spillover_pid_analysis3$treat)
spillover_pid_analysis3$match_top1_nobs_interact <- as.numeric(spillover_pid_analysis3$match_top1_all_n_obs * spillover_pid_analysis3$treat)
spillover_pid_analysis3$match_top1_dist_interact <- as.numeric(spillover_pid_analysis3$match_top1_all_dist * spillover_pid_analysis3$treat)
spillover_pid_analysis3$match_top1_dist_interact3 <- as.numeric(spillover_pid_analysis3$match_top1_all_rival*spillover_pid_analysis3$match_top1_all_dist * spillover_pid_analysis3$treat)

spillover_pid_analysis4$match_top1_single <- as.numeric(spillover_pid_analysis4$match_top1_all_n_obs == 1)
spillover_pid_analysis4$match_top1_single_interact <- as.numeric(spillover_pid_analysis4$match_top1_single * spillover_pid_analysis4$treat)
spillover_pid_analysis4$match_top1_nobs_interact <- as.numeric(spillover_pid_analysis4$match_top1_all_n_obs * spillover_pid_analysis4$treat)
spillover_pid_analysis4$match_top1_dist_interact <- as.numeric(spillover_pid_analysis4$match_top1_all_dist * spillover_pid_analysis4$treat)
spillover_pid_analysis4$match_top1_dist_interact3 <- as.numeric(spillover_pid_analysis4$match_top1_all_rival*spillover_pid_analysis4$match_top1_all_dist * spillover_pid_analysis4$treat)


# Write function ----------------------"

pdf <- function(...) invisible(NULL)
dev.off <- function(...) {
  tryCatch(
    expr = grDevices::dev.off(...),
    error = function(e) invisible(NULL)
  )
}



# Load plot helpers --------------------------------------------------

source("9_graph_helpers.R")


# Create plots -----------------------------------------------

#Figure A8

plot_binning(
  all_analysis2,
  adjusted_density = inter_binning_density(D = "treat",
                                           X = "match_top1_all_rival",
                                           cutoffs = c(0.01,0.99),
                                           data = spillover_pid_analysis2,
                                           weights = "w")
)
ggsave("all_binning_plot_density.pdf", width = 5.6, height = 4, units = "in")




#Table A3

modelI_robustness<-lm_robust(marked_register_2014~treat*street_experiment_share+treat*street_turnout_share_2013+factor(ward), clusters=street, weights = w, data=spillover_pid2)

summary(modelI_robustness)

modelII_robustness<-lm_robust(marked_register_2014~treat*street_experiment_share+treat*street_turnout_share_2013+treat*street_rival_share+factor(ward), clusters=street, weights = w, data=spillover_pid2)

summary(modelII_robustness)

modelIII_robustness<-lm_robust(marked_register_2014~treat*street_experiment_share+treat*street_turnout_share_2013+factor(ward), clusters=street, weights = w, data=spillover_pid3)

summary(modelIII_robustness)

modelIV_robustness<-lm_robust(marked_register_2014~treat*street_experiment_share+treat*street_turnout_share_2013+treat*street_rival_share+factor(ward) , clusters=street, weights = w, data=spillover_pid3)

summary(modelIV_robustness)

modelV_robustness<-lm_robust(marked_register_2014~treat*street_experiment_share+treat*street_turnout_share_2013+factor(ward), clusters=street, weights = w, data=spillover_pid4)

summary(modelV_robustness)

modelVI_robustness<-lm_robust(marked_register_2014~treat*street_experiment_share+treat*street_turnout_share_2013+treat*street_rival_share+factor(ward), clusters=street, weights = w, data=spillover_pid4)

summary(modelVI_robustness)

tableA3<-texreg(list(modelI_robustness, modelII_robustness, modelIII_robustness, modelIV_robustness, modelV_robustness, modelVI_robustness), include.ci = FALSE,  include.rmse = FALSE, include.rsquared = FALSE, omit.coef = "(ward)|(match)", include.adjrs = FALSE, include.bic=FALSE, digits = 3)

tableA3


#Table A4

spillover_pid_analysis2$pid_cat[spillover_pid_analysis2$match_top1_all_rival==0]<-1
spillover_pid_analysis2$pid_cat[spillover_pid_analysis2$match_top1_all_rival>0 & spillover_pid_analysis2$match_top1_all_rival<1]<-2
spillover_pid_analysis2$pid_cat[spillover_pid_analysis2$match_top1_all_rival==1]<-3

spillover_pid_analysis3$pid_cat[spillover_pid_analysis3$match_top1_all_rival==0]<-1
spillover_pid_analysis3$pid_cat[spillover_pid_analysis3$match_top1_all_rival>0 & spillover_pid_analysis3$match_top1_all_rival<1]<-2
spillover_pid_analysis3$pid_cat[spillover_pid_analysis3$match_top1_all_rival==1]<-3

spillover_pid_analysis4$pid_cat[spillover_pid_analysis4$match_top1_all_rival==0]<-1
spillover_pid_analysis4$pid_cat[spillover_pid_analysis4$match_top1_all_rival>0 & spillover_pid_analysis4$match_top1_all_rival<1]<-2
spillover_pid_analysis4$pid_cat[spillover_pid_analysis4$match_top1_all_rival==1]<-3



model1<-lm_robust(marked_register_2014~treat*factor(pid_cat)+factor(ward)+match_top1_single+ match_top1_single_interact + match_top1_all_n_obs+ match_top1_nobs_interact+match_top1_all_dist, clusters=street, weights = w, data=spillover_pid_analysis2)

summary(model1)


model2<-lm_robust(marked_register_2014~treat*factor(pid_cat)+factor(ward)+match_top1_single+ match_top1_single_interact + match_top1_all_n_obs+ match_top1_nobs_interact+match_top1_all_dist, clusters=street, weights = w, data=spillover_pid_analysis3)

summary(model2)


model3<-lm_robust(marked_register_2014~treat*factor(pid_cat)+factor(ward)+match_top1_single+ match_top1_single_interact + match_top1_all_n_obs+ match_top1_nobs_interact+match_top1_all_dist, clusters=street, weights = w, data=spillover_pid_analysis4)

summary(model3)


texreg(list(model1, model2, model3), include.ci = FALSE,  include.rmse = FALSE, include.rsquared = FALSE, omit.coef = "(ward)|(match)" , include.adjrs = FALSE, include.bic=FALSE, digits = 3)



#Figure A9

#Figure A9a

pdf(paste("Figure_all_analysis1_cov.pdf"),w=7,h=5)
inter.binning(Y = "marked_register_2014", D = "treat", X="street_rival_share", Z=c("match_top1_all_dist","marked_register_2013_nomiss", "addr_hh_size", "male") , FE=("ward"), cl="street", bin.labs=FALSE,Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", data = spillover_pid2, weights = "w", na.rm = TRUE)
dev.off()

#Figure A9b

pdf(paste("Figure_Lab_analysis1_cov.pdf"),w=7,h=5)
inter.binning(Y = "marked_register_2014", D = "treat", X="street_rival_share", Z=c("street_experiment_share","marked_register_2013_nomiss", "addr_hh_size", "male") , FE=("ward"), cl="street", bin.labs=FALSE,Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", data = spillover_pid3, weights = "w")
dev.off()


#Figure A9c
pdf(paste("Figure_rival_analysis1_cov.pdf"),w=7,h=5)
inter.binning(Y = "marked_register_2014", D = "treat", X="street_rival_share", Z=c("street_experiment_share","marked_register_2013_nomiss", "addr_hh_size", "male"), FE=("ward"), cl="street", bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", data = spillover_pid4, weights = "w")
dev.off()



#Figure A10

#Figure A10a

pdf(paste("Figure_all_analysis1_kernel.pdf"),w=7,h=5)
inter.kernel(Y = "marked_register_2014", D = "treat", X="street_rival_share", cl="street",  Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", bw=.3, data = spillover_pid2[spillover_pid2$street_rival_share<.65, ], weights = "w")
dev.off()

pdf(paste("Figure_all_analysis1_raw.pdf"),w=7,h=5)
inter.raw(Y = "marked_register_2014", D = "treat", X="street_rival_share", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  span=.6,  data = spillover_pid2)
dev.off()


#Figure A10b

pdf(paste("Figure_Lab_analysis1_kernel.pdf"),w=7,h=5)
inter.kernel(Y = "marked_register_2014", D = "treat", X="street_rival_share", cl="street", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", bw=.3, data = spillover_pid3, weights = "w")
dev.off()

pdf(paste("Figure_Lab_analysis1_raw.pdf"),w=7,h=5)
inter.raw(Y = "marked_register_2014", D = "treat", X="street_rival_share", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  span=.6,  data = spillover_pid3)
dev.off()

#Figure A10c


pdf(paste("Figure_rival_analysis1_kernel.pdf"),w=7,h=5)
inter.kernel(Y = "marked_register_2014", D = "treat", X="street_rival_share",  cl="street", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", bw=.3, data = spillover_pid4, weights = "w")
dev.off()


pdf(paste("Figure_rival_analysis1_raw.pdf"),w=7,h=5)
inter.raw(Y = "marked_register_2014", D = "treat", X="street_rival_share", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  span=.6,  data = spillover_pid4)
dev.off()


#Figure A11

all_analysis2_cov<- inter.binning(Y = "marked_register_2014", D = "treat", X="match_top1_all_rival", Z=c("match_top1_all_dist", "marked_register_2013_nomiss", "addr_hh_size" , "male", "match_top1_single", "match_top1_single_interact", "match_top1_all_n_obs", "match_top1_nobs_interact"), bin.labs=FALSE, FE=("ward"), cl="street", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters in closest household",  cutoffs = c(0.01,0.99), Xdistr = "density", data = spillover_pid_analysis2, weights = "w")

lab_analysis2_cov<-inter.binning(Y = "marked_register_2014", D = "treat", X="match_top1_all_rival", Z=c("match_top1_all_dist", "marked_register_2013_nomiss",  "addr_hh_size", "male", "match_top1_single", "match_top1_single_interact", "match_top1_all_n_obs", "match_top1_nobs_interact"), bin.labs=FALSE, FE=("ward"), cl="street", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters in closest household",  cutoffs = c(0.01,0.99), Xdistr = "density", data = spillover_pid_analysis3, weights = "w")

rival_analysis2_cov<- inter.binning(Y = "marked_register_2014", D = "treat", X="match_top1_all_rival",  Z=c("match_top1_all_dist", "marked_register_2013_nomiss", "addr_hh_size", "male", "match_top1_single", "match_top1_single_interact", "match_top1_all_n_obs", "match_top1_nobs_interact"), bin.labs=FALSE, FE=("ward"), cl="street", Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters in closest household",  cutoffs = c(0.01,0.99), Xdistr = "density", data = spillover_pid_analysis4, weights = "w")



# Create plots -----------------------------------------------

#Figure A11a

plot_binning(
  all_analysis2_cov,
  adjusted_density = inter_binning_density(D = "treat",
                                           X = "match_top1_all_rival",
                                           cutoffs = c(0.01,0.99),
                                           data = spillover_pid_analysis2,
                                           weights = "w")
)
ggsave("all_binning_plot_density_robust.pdf", width = 5.6, height = 4, units = "in")


#Figure A11b
plot_binning(
  lab_analysis2_cov,
  adjusted_density = inter_binning_density(D = "treat",
                                           X = "match_top1_all_rival",
                                           cutoffs = c(0.01,0.99),
                                           data = spillover_pid_analysis3,
                                           weights = "w")
)
ggsave("Lab_binning_plot_density_robust.pdf", width = 5.6, height = 4, units = "in")

#Figure A11c

plot_binning(
  rival_analysis2_cov,
  adjusted_density = inter_binning_density(D = "treat",
                                           X = "match_top1_all_rival",
                                           cutoffs = c(0.01,0.99),
                                           data = spillover_pid_analysis4,
                                           weights = "w")
)
ggsave("rival_binning_plot_density_robust.pdf", width = 5.6, height = 4, units = "in")
